<template>
  <div>
    <el-row :gutter="15">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-col :span="14">
          <el-form-item label="用户名称" prop="userName">
            <el-input v-model="formData.userName" placeholder="邮箱或者手机号用户名称" clearable
              prefix-icon='el-icon-user-solid' :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="14">
          <el-form-item label="密码" prop="userPwd">
            <el-input v-model="formData.userPwd" placeholder="请输入密码" clearable prefix-icon='el-icon-star-on'
              show-password :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="性别" prop="userSex">
            <el-radio-group v-model="formData.userSex" size="medium">
              <el-radio-button v-for="(item, index) in userSexOptions" :key="index" :label="item.value"
                :disabled="item.disabled">{{item.label}}</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="14">
          <el-form-item label="手机号" prop="userPhone">
            <el-input v-model="formData.userPhone" placeholder="请输入手机号" clearable prefix-icon='el-icon-phone'
              :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="选择头像" prop="userImg" required>
            <el-upload ref="userImg" 
            :file-list="userImgfileList" 
            :on-success="success" 
            :action="userImgAction"
            :before-upload="userImgBeforeUpload" 
            list-type="picture-card" accept="image/*">
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="Email" prop="userEmail">
            <el-input v-model="formData.userEmail" placeholder="请输入Email" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item size="large">
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
  </div>
</template>
<script>
export default {
  components: {},
  props: [],
  data() {
    return {
      formData: {
        userName: undefined,
        userPwd: undefined,
        userSex: 2,
        userPhone: undefined,
        userImg: null,
        userEmail: undefined,
      },
      rules: {
        userName: [{
          required: true,
          message: '邮箱或者手机号用户名称',
          trigger: 'blur'
        }],
        userPwd: [{
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        }],
        userSex: [{
          required: true,
          message: '性别不能为空',
          trigger: 'change'
        }],
        userPhone: [{
          required: true,
          message: '请输入手机号',
          trigger: 'blur'
        }, {
          pattern: /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/,
          message: '请输入正确的手机号',
          trigger: 'blur'
        }],
        userEmail: [{
          required: true,
          message: '请输入Email',
          trigger: 'blur'
        }, {
          pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
          message: '请输入正确的邮箱',
          trigger: 'blur'
        }],
      },
      userImgAction: 'https://localhost:44347/Upload',
      userImgfileList: [],
      userSexOptions: [{
        "label": "男",
        "value": 1
      }, {
        "label": "女",
        "value": 2
      }],
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    submitForm() {
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        // TODO 提交表单
        this.axios
        .post("https://localhost:44347/api/app/p-user/roll",this.formData)
        .then(s=>{
          if(s.data==10)
          {
            alert("添加成功");

          }
          else
          {
            alert("注册失败")
          }
        })
      })
    },
    success(s,file)
    {
      this.formData.userImg=file.response;
    },

    resetForm() {
      this.$refs['elForm'].resetFields()
    },
    userImgBeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 / 1024 < 2
      if (!isRightSize) {
        this.$message.error('文件大小超过 2GB')
      }
      let isAccept = new RegExp('image/*').test(file.type)
      if (!isAccept) {
        this.$message.error('应该选择image/*类型的文件')
      }
      return isRightSize && isAccept
    },
  }
}

</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}

</style>
